import { base_url } from './config.js';

export function getCode() {
  return new Promise((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success(res) {
        console.log('【 Response in request wx.login 】', res);
        resolve(res.code);
      },
      fail(err) {
        console.log('【 Error in request wx.login 】', err);
        reject('获取微信 code 失败');
      }
    });
  });
}

export function loginByCode(code) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + '/login/wx-login',
      method: 'POST',
      data: {
        code,
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log('【 Response in request wxLoginBycode 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request wxLoginBycode 】', err);
        reject(err);
      }
    });
  });
}

export function updateUserInfo(sex, grade, college, subCollege, major, username) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + '/user/userinfo',
      method: 'PUT',
      data: {
        sex, grade, college, subCollege, major, username,
      },
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      success(res) {
        console.log('【 Response in request updateUserInfo 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request updateUserInfo 】', err);
        reject(err);
      }
    });
  });
}

export function getUserInfo() {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + '/user/userinfo',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      success(res) {
        console.log('【 Response in request getUserInfo 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request getUserInfo 】', err);
        reject(err);
      }
    });
  });
}

export function getFollowInfo() {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + '/user/follow-count',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      success(res) {
        console.log('【 Response in request getFollowInfo 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request getFollowInfo 】', err);
        reject(err);
      }
    });
  });
}

export function changeUserAvatar(dataurl) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + '/user/avatar',
      method: 'PUT',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      data: {
        img: dataurl,
      },
      success(res) {
        console.log('【 Response in request changeUserAvatar 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request changeUserAvatar 】', err);
        reject(err);
      }
    });
  });
}

export function changeUserInfo(newData) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + '/user/userinfo',
      method: 'PUT',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      data: newData,
      success(res) {
        console.log('【 Response in request changeUserInfo 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request changeUserInfo 】', err);
        reject(err);
      }
    });
  });
}